/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.hotpath;

import java.awt.event.*;

import org.apache.commons.vfs.*;

import fileCommodore.plugin.*;

/**
 * @author nado18@gmail.com, on Aug 1, 2007
 */
public class HotPathPlugin extends Plugin
{
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#activeSelectionChanged()
	 */
	@Override
	public void activeSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#handle(java.awt.event.KeyEvent)
	 */
	@Override
	public void handle( KeyEvent ev )
	{
		if (isHotPathKey( ev ))
		{
			handleHotPathKeys( ev );
		}
	}
	
	private boolean isHotPathKey( KeyEvent e )
	{
		int code = e.getKeyCode();
		boolean retval =
				e.isControlDown()
						&& !e.isAltDown()
						&& !e.isAltGraphDown()
						&& !e.isMetaDown()
						&& ( KeyEvent.VK_0 == code || KeyEvent.VK_1 == code
								|| KeyEvent.VK_2 == code
								|| KeyEvent.VK_3 == code
								|| KeyEvent.VK_4 == code
								|| KeyEvent.VK_5 == code
								|| KeyEvent.VK_6 == code
								|| KeyEvent.VK_7 == code
								|| KeyEvent.VK_8 == code || KeyEvent.VK_9 == code );
		
		return retval;
	}
	
	private int extractHotPathIndex( KeyEvent e )
	{
		switch (e.getKeyCode())
		{
			case KeyEvent.VK_0:
				return 0;
			case KeyEvent.VK_1:
				return 1;
			case KeyEvent.VK_2:
				return 2;
			case KeyEvent.VK_3:
				return 3;
			case KeyEvent.VK_4:
				return 4;
			case KeyEvent.VK_5:
				return 5;
			case KeyEvent.VK_6:
				return 6;
			case KeyEvent.VK_7:
				return 7;
			case KeyEvent.VK_8:
				return 8;
			case KeyEvent.VK_9:
				return 9;
			default:
				return -1;
		}
	}
	
	private void handleHotPathKeys( KeyEvent e )
	{
		if (isHotPathKey( e ))
		{
			int index = extractHotPathIndex( e );
			
			if (-1 != index)
			{
				if (e.isShiftDown())
				{
					saveHotPath( index );
				}
				else
				{
					try
					{
						jumpToHotPath( index );
					}
					catch (Exception e1)
					{
						showError(
							String.format(
								"There was an error changing to hot path #%c.\r\n\r\n%s",
								e.getKeyChar(),
								e1 ),
							"Hot Path Error" );
					}
				}
			}
		}
	}
	
	private void saveHotPath( int num )
	{
		FileObject current = getActiveSelection().getCurrent().getFileObject();
		if (num >= 0 && num < 10 && null != current)
		{
			String key = Integer.toString( num );
			String value = current.getName().getURI();
			setProperty( key, value );
		}
	}
	
	private void jumpToHotPath( int num ) throws FileSystemException
	{
		FileObject current = getActiveSelection().getCurrent().getFileObject();
		if (num >= 0 && num < 10 && null != current)
		{
			String value = getProperty( Integer.toString( num ) );
			if (value != null && value.length() > 0)
			{
				changeActiveTo( value );
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#inactiveSelectionChanged()
	 */
	@Override
	public void inactiveSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
}
